<?php

namespace Morrow;

class PageController extends DefaultController {
	public function run() {

		$this->form->loadDef(
			array('myform_one' => array(

					"username" => array(
		            	"type" => "text",
	            	),

            	)
            )
        );




		$this->form->loadDef(
			array('myform_two' => array(

					"username" => array(
		            	"type" => "text",
	            	),

		            "oldpassword" => array(
		            	"type" => "text",
	            	),

	            	"password" => array(

		            	"type" => "text",
		            	"displaytype" => "password",
		            	"checktype" => "password",
		            	"compare" => "password2",
		            	"label" => "password :",


	            	),

	            	"password2" => array(
	            		"type" => "text",
	            		"displaytype" => "password",
	            	)
            	)
            )
        );

		$this->form->loadDef(
			array('myform_three' => array(

					"name" => array(
		            	"type" => "text",
	            	),

					"org" => array(
		            	"type" => "text",
	            	),
					"cardemail" => array(
		            	"type" => "text",
	            	),
					"tel" => array(
		            	"type" => "text",
	            	),
					"mobile" => array(
		            	"type" => "text",
	            	),
					"adresse" => array(
		            	"type" => "text",
	            	),
					"zip" => array(
		            	"type" => "text",
	            	),
					"city" => array(
		            	"type" => "text",
	            	),
					"country" => array(
		            	"type" => "text",
	            	),	            	
					"jiami" => array(
		            	"type" => "text",
	            	),
            	)
            )
        );


		if(!$this->session->get("uid")){
			$this -> url -> redirect('home');
		}
		else{
			$uid=$this->session->get("uid");
		}

		
		$this->form->setInput($this->input->get());

		//form_one
        $this->load('Db', $this->config->get('db'));                
        $sql = $this->db->result('
			SELECT *
			FROM users
			WHERE uid = ?'
			, $uid);


        $this->view->setContent($sql['RESULT'][0], 'data');

 		if($this -> form -> isSubmitted('myform_one')) {
    			$valid = $this -> form -> validate('myform_one');

            	$this->view->setContent($this->form->getErrors('myform_one'), "error");

    				if ($valid) {
        		        $formdata = $this -> form -> getValues('myform_one');

                       	$this->db->updateSafe('users', $formdata , 'where uid = ?', true, $uid);
						$this->session->set('messenger','changename1');
		        		//var_dump($data);
	        			$this -> url -> redirect('user');
                                              
    				} 
    				else {
    				}
		}     	  

		//form_two

		if($this -> form -> isSubmitted('myform_two')) {
    			$valid = $this -> form -> validate('myform_two');

            	$this->view->setContent($this->form->getErrors('myform_two'), "error");

    				if ($valid) {
        		        $formdata = $this -> form -> getValues('myform_two');
        			
                        $sql = $this->db->result('
							SELECT password
							FROM users
							WHERE uid = ?'
							, $uid);

                        $password = $sql["RESULT"][0]["password"];

                        if($password !== md5($formdata["oldpassword"])){
                        	$this->form->setError('myform_two','oldpassword',$this->language->_('旧的密码不对') );
                        }
                        else{

                        	$data=array('password'=>md5($formdata['password']));

                        	$this->db->updateSafe('users', $data, 'where uid = ?', true, $uid);

							$this->session->set('messenger','changepassword1');
		        			//var_dump($data);
		        			$this -> url -> redirect('user');


                        }                                              

    				} 
    				else {
    				}
		}

		//form_three
 		if($this -> form -> isSubmitted('myform_three')) {
    			$valid = $this -> form -> validate('myform_three');

            	$this->view->setContent($this->form->getErrors('myform_three'), "error");

    				if ($valid) {
        		        $formdata = $this -> form -> getValues('myform_three');

                       	$this->db->updateSafe('users', $formdata , 'where uid = ?', true, $uid);
						$this->session->set('messenger','changecontact1');
		        		//var_dump($data);
	        			$this -> url -> redirect('user');
                                              
    				} 
    				else {
    				}
		} 

	}	
}